package com.mflex.mwms.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.JlrCompareAdapter;
import com.mflex.mwms.adapter.TeslaCompareAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.JLRPalletBox;
import com.mflex.mwms.model.Task;
import com.mflex.mwms.model.TaskDetail;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class JlrCompareActivity extends BaseActivity  implements AdapterView.OnItemClickListener{
    @BindView(R.id.stv_TaskNo)
    EditText stvTaskNo;

    @BindView(R.id.lvTaskList)
    ListView lvTaskList;

    MiniLoadingDialog mMiniLoadingDialog;

    List<String> taskList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jlr_compare);

        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        taskList = new ArrayList<>();
        JlrCompareAdapter jlrCompareAdapter = new JlrCompareAdapter(this, taskList);
        lvTaskList.setAdapter(jlrCompareAdapter);

        stvTaskNo.setOnKeyListener(this);
        lvTaskList.setOnItemClickListener(this);
        stvTaskNo.requestFocus();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_TaskNo:
                    requestTask(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void requestTask(View view) {
        if (StringUtils.isEmpty(stvTaskNo.getText().toString())) {
            return;
        }
        mMiniLoadingDialog.show();
        XHttp.get("api/Task/GetPalletByTaskNo")
                .params("TaskNo", stvTaskNo.getText().toString())
                .params("WareHouseName", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<List<String>>, List<String>>(new SimpleCallBack<List<String>>() {

                    @Override
                    public void onSuccess(List<String> response) {
                        mMiniLoadingDialog.dismiss();
                        taskList = response;
                        JlrCompareAdapter jlrCompareAdapter = new JlrCompareAdapter(JlrCompareActivity.this, taskList);
                        lvTaskList.setAdapter(jlrCompareAdapter);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvTaskNo.requestFocus();
                        taskList = new ArrayList<>();
                        JlrCompareAdapter jlrCompareAdapter = new JlrCompareAdapter(JlrCompareActivity.this, taskList);
                        lvTaskList.setAdapter(jlrCompareAdapter);
                        MessageUtils.errorMsgAndVoice(JlrCompareActivity.this, e);
                    }
                }) {
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        requestTask(JlrCompareActivity.this.getCurrentFocus());
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        mMiniLoadingDialog.show();
        XHttp.get("api/Task/GetBoxByPallet")
                .params("Pallet", taskList.get(i))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<List<JLRPalletBox>>, List<JLRPalletBox>>(new SimpleCallBack<List<JLRPalletBox>>() {

                    @Override
                    public void onSuccess(List<JLRPalletBox> response) {
                        mMiniLoadingDialog.dismiss();
                        Intent intent = new Intent(JlrCompareActivity.this, JlrCompareDetailActivity.class);
                        intent.putExtra("JLRPALLETBOX", JsonUtil.toJson(response));
                        startActivityForResult(intent, 0);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MessageUtils.errorMsgAndVoice(JlrCompareActivity.this, e);
                    }
                }) {
                });


    }
}